package glory.xxd

import org.apache.activemq.ActiveMQConnectionFactory
import org.apache.commons.logging.LogFactory

import javax.jms.*

/**
 *
 * Created by Administrator on 2018-01-23.
 */
class ProducerUtil {

    private final static logger = LogFactory.getLog(this)

    private static final String USERNAME = "admin";

    private static final String PASSWORD = "admin";

    //private static final String BROKEURL = "tcp://127.0.0.1:61616"

//     private static final String BROKEURL ="tcp://10.10.10.44:61620"
    //172.5.153.99
    private static final String BROKEURL = "tcp://127.0.0.1:61620"
    //private static final String BROKEURL = "tcp://172.5.153.99:61624"


    /**
     *
     * @param msgId
     * @param msgName
     * @param msgInfo
     * @return
     */
    public static String sendQueueMsg(String msgId, String msgName, String msgInfo) {
        logger.info(msgInfo)
        String msg = "-200";
        Connection connection
        Session session
        try {
            //println "mesinfo:   "+msgInfo
            ConnectionFactory connectionFactory = new ActiveMQConnectionFactory( USERNAME,  PASSWORD, BROKEURL);
            connection = connectionFactory.createConnection();
            connection.start();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            Destination destination = session.createQueue(msgName);
            MessageProducer messageProducer = session.createProducer(destination);
            messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT)

            TextMessage message = session.createTextMessage();
            message.setText(msgInfo)
            message.setJMSCorrelationID(msgId)
            //message.setJMSMessageID(msgId)
            messageProducer.send(message);
            //session.commit();
            msg = "200";
            logger.info( "-------------${new Date()}send Queue:${ProducerUtil.BROKEURL}connect sucess!MSGID:${msgId}---------------")
        } catch (Exception e) {
            logger.error("-------------${new Date()}send Queue:${ProducerUtil.BROKEURL}connect  error!MSGID:${msgId}---------------")
            logger.error(e.getMessage())
        }finally{
            if(connection){
                connection.close();
            }
            if(session){
                session.close()
            }
            return msg;
        }
    }

    /**
     * sendTopicMsg
     * @param msgId
     * @param msgName
     * @param msgInfo
     * @return
     */
    public static String sendTopicMsg(String msgId, String msgName, String msgInfo) {

        String msg = "-200";
        logger.info(msgInfo)
        Connection connection
        Session session
        try {
            ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ProducerUtil.USERNAME, ProducerUtil.PASSWORD, ProducerUtil.BROKEURL);
            connection = connectionFactory.createConnection();
            connection.start();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            Destination destination = session.createTopic(msgName);
            MessageProducer messageProducer = session.createProducer(destination);
            //设置非持久化
            messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT)

            TextMessage message = session.createTextMessage();
            message.setText(msgInfo)
            message.setJMSCorrelationID(msgId)

            //message.setJMSMessageID(msgId)
            messageProducer.send(message);
            msg = "200";
            logger.info( "-------------${new Date()}send Topic:${ConsumerUtil.BROKEURL}success!MSGID:${msgId}---------------")
        } catch (Exception e) {
            logger.info( "-------------${new Date()}send Topic:${ConsumerUtil.BROKEURL}  error!MSGID:${msgId}---------------")
            logger.info(e.toString())
        }finally{
            if(connection){
                connection.close();
            }
            if(session){
                session.close()
            }
            return msg;
        }
    }
}
